package code;

import sun.applet.Main;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
 * 岛屿面积问题
 * @param: 
 * @return: 
 * @author: huang
 * @date: 2023/4/13 16:25
 */
public class Mian_0411 {
    int[][] toolArr = new int[][]{
            {0, 1},
            {1, 0},
            {0, -1},
            {-1, 0}};

    public int maxAreaOfIsland(int[][] grid) {

        List<String> list = new ArrayList<>();
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    list.add(i + "_" + j);
                }
            }
        }
        int max = 0;
        while (list.size() > 0) {
            int tem = list.size();
            dfs(list, list.get(0));
            max = Math.max(max, tem - list.size());
        }
        System.out.println(max);
        return max;
    }

    private void dfs(List<String> list, String point) {
        list.remove(point);
        String[] split = point.split("_");
        int y = Integer.valueOf(split[0]);
        int x = Integer.valueOf(split[1]);
        for (int[] ints : toolArr) {
            int temy = y + ints[0];
            int temx = x + ints[1];
            if (list.contains(temy + "_" + temx)) {
                dfs(list, temy + "_" + temx);
            }
        }
    }

    public static void main(String[] args) {
        int[][] a = new int[][]{{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0}, {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}};
        Mian_0411 mian_0411 = new Mian_0411();
        mian_0411.maxAreaOfIsland(a);

    }

}
